# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smalleel-api-tools
# FileName:     read_config.py
# Author:
# Datetime:    2022/7/18 17:04
# Description：封装读取配置
# -----------------------------------------------------------------------------------
import configparser
import os


class ConfigUtil:
    def __init__(self):
        self.config = configparser.ConfigParser()
        if __file__:
            self.beforePath = os.path.dirname(os.path.abspath(__file__)).split("common")[0]
        # 找到ini文件路径
        self.__dataBaseInfoFilePath = str(self.beforePath) + r"common/config/dataBase_config.ini"
        self.__configFilePath = str(self.beforePath) + r"common/config/path_config.ini"
        self.__userConfigFilepath = str(self.beforePath) + r"common/config/user_config.ini"
        self.__ftpConfigFilepath = str(self.beforePath) + r"common/config/ftp_config.ini"
        self.__apiConfigFilepath = str(self.beforePath) + r"common/config/api_config.ini"

    def get_excel_path(self, option):
        self.config.read(self.__configFilePath, encoding="utf-8")
        filePath = self.config.get("Excel_Path", option)
        return os.path.join(self.beforePath, filePath)

    def get_json_path(self, option: str) -> str:
        """
        :param option: path_config.ini文件对应的json文件路径key
        :return:json文件的路径
        """
        self.config.read(self.__configFilePath, encoding="utf-8")
        filePath = self.config.get("Json_Path", option)
        return os.path.join(self.beforePath, filePath)

    def get_database_status(self) -> bool:
        """
            ;True：线上环境
            ;False：测试环境
        """
        self.config.read(self.__dataBaseInfoFilePath, encoding="utf-8")
        status = self.config.get("Database_Info", "dataBaseStatus")
        if status.lower() == "true":
            return True
        else:
            return False

    def get_database_info(self, option: str) -> str:
        if self.get_database_status() is True:
            return self.config.get("Database_Pro", option)
        else:
            return self.config.get("Database_Test", option)

    def get_log_path(self, option: str) -> str:
        """
            path_config.ini
        :param option: 文件里的路径key例如：smalleelLogPath
        :return: 日志文件的路径
        """
        self.config.read(self.__configFilePath, encoding="utf-8")
        filePath = self.config.get("log_Path", option)
        return os.path.join(self.beforePath, filePath)

    def get_user_info(self, option: str) -> str:
        """
            user_config.ini
        :param option: 文件的key
        :return: 账号密码
        """
        self.config.read(self.__userConfigFilepath, encoding="utf-8")
        return self.config.get("User_Info", option)

    def get_ftp_info(self, option: str):
        """
            ftp_config.ini
        :param option: 文件的key
        :return: 账号密码
        """

        self.config.read(self.__ftpConfigFilepath, encoding="utf-8")
        return self.config.get("Ftp_Info", option)

    def get_html_path(self, option: str):
        """
            ftp_config.ini
        :param option: 文件的key
        :return: 账号密码
        """

        self.config.read(self.__configFilePath, encoding="utf-8")
        return os.path.join(self.beforePath, self.config.get("Html_Path", option))

    def get_downFile_path(self, option: str):
        self.config.read(self.__configFilePath, encoding="utf-8")
        return os.path.join(self.beforePath, self.config.get("Down_Path", option))

    def get_importFile_path(self, option: str):
        self.config.read(self.__configFilePath, encoding="utf-8")
        return os.path.join(self.beforePath, self.config.get("Import_Path", option))

    def get_serverIp(self, option: str = "serverIp"):
        self.config.read(self.__ftpConfigFilepath, encoding="utf-8")
        return self.config.get("Ip_Info", option)

    def get_ipUrl(self, ):
        self.config.read(self.__apiConfigFilepath, encoding="utf-8")
        if self.get_database_status() is True:
            return self.config.get("Api_Path", "ipUrlPro")
        else:
            return self.config.get("Api_Path", "ipUrlTest")

    def get_timeout_config(self, ):
        self.config.read(self.__apiConfigFilepath, encoding="utf-8")
        return self.config.get("Api_Config", "timeoutConfig")


if __name__ == '__main__':
    readIni = ConfigUtil()
    print(readIni.get_ipUrl())
